package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SuperProduct;
import com.ruoyi.system.domain.SuperPromotion;
import com.ruoyi.system.service.ISuperPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 促销活动Controller
 * 
 * @author xjf
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/activity/promotion")
public class SuperPromotionController extends BaseController
{
    @Autowired
    private ISuperPromotionService superPromotionService;

    /**
     * 查询促销活动列表
     */
    @PreAuthorize("@ss.hasPermi('activity:promotion:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuperPromotion superPromotion,
                              @RequestParam(required = false) Long fatherCategoryId) {  // 接收前端的 categoryId
        // 如果传递了 categoryId，设置到 product 对象中
        if (fatherCategoryId != null) {
            if (superPromotion.getProduct() == null) {
                superPromotion.setProduct(new SuperProduct());  // 初始化 product 对象，避免空指针
            }
            superPromotion.getProduct().setFatherCategoryId(fatherCategoryId);  // 设置分类ID到 product 中
        }

        startPage();
        List<SuperPromotion> list = superPromotionService.selectSuperPromotionList(superPromotion);
        return getDataTable(list);
    }
    /**
     * 导出促销活动列表
     */
    @PreAuthorize("@ss.hasPermi('activity:promotion:export')")
    @Log(title = "促销活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuperPromotion superPromotion)
    {
        List<SuperPromotion> list = superPromotionService.selectSuperPromotionList(superPromotion);
        ExcelUtil<SuperPromotion> util = new ExcelUtil<SuperPromotion>(SuperPromotion.class);
        util.exportExcel(response, list, "促销活动数据");
    }

    /**
     * 获取促销活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:promotion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(superPromotionService.selectSuperPromotionById(id));
    }

    /**
     * 新增促销活动
     */
    @PreAuthorize("@ss.hasPermi('activity:promotion:add')")
    @Log(title = "促销活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SuperPromotion superPromotion)
    {
        return toAjax(superPromotionService.insertSuperPromotion(superPromotion));
    }

    /**
     * 修改促销活动
     */
    @PreAuthorize("@ss.hasPermi('activity:promotion:edit')")
    @Log(title = "促销活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SuperPromotion superPromotion)
    {
        return toAjax(superPromotionService.updateSuperPromotion(superPromotion));
    }

    /**
     * 删除促销活动
     */
    @PreAuthorize("@ss.hasPermi('activity:promotion:remove')")
    @Log(title = "促销活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(superPromotionService.deleteSuperPromotionByIds(ids));
    }
}
